﻿Public Class HelpSystem
    Private o_Help As HelpFileCollection
    Private HelpPath As String = ""
    Public Sub LoadItems(ByVal HelpPath As String, ByVal HelpListPath As String)
        o_Help = New HelpFileCollection
        o_Help.LoadList(HelpListPath)
        Me.HelpPath = HelpPath
        LoadTree()
        ' ListHelpFiles.DataSource = o_Help
    End Sub
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'o_Help = New HelpFileCollection

        ' LoadItems()
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        o_Help = Nothing
    End Sub
    Private Function LoadHelpPage(ByVal PageName As String, ByVal Title As String) As WebBrowser
        Dim TTab As TabPage
        Dim TWeb As New WebBrowser
        TTab = New TabPage
        TTab.Name = PageName
        TTab.Text = Title
        TTab.Font = New Font("Arial", 10, FontStyle.Regular)
        TWeb.Dock = DockStyle.Fill
        TWeb.ScriptErrorsSuppressed = True
        'TTab.ContextMenuStrip = ContextMenuStrip1
        TTab.Controls.Add(TWeb)
        TabControl1.TabPages.Add(TTab)
        TabControl1.SelectedTab = TTab
        Return TWeb
    End Function
    Private Function GetActiveWebControl() As WebBrowser
        If Me.TabControl1.SelectedTab IsNot Nothing Then
            Dim Tweb As WebBrowser = CType(Me.TabControl1.SelectedTab.Controls.Item(0), WebBrowser)
            If Tweb IsNot Nothing Then
                Return Tweb
            End If
        End If
        Return Nothing
    End Function
    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        If TabControl1.SelectedTab IsNot Nothing Then
            TabControl1.TabPages.Remove(TabControl1.SelectedTab)
        End If
    End Sub
    ''' <summary>
    ''' Load our tree
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadTree()
        Me.HelpTree.Nodes.Clear()
        Dim o_RootNode As TreeNode = Me.HelpTree.Nodes.Add("Help")
        For Each o_ItemHelp As HelpList In Me.o_Help
            AddNode(o_RootNode, o_ItemHelp)
        Next
        o_RootNode.ExpandAll()
    End Sub
    ''' <summary>
    ''' Load the Treeview with help
    ''' </summary>
    ''' <param name="o_TreeNode"></param>
    ''' <param name="o_HelpItem"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function AddNode(ByVal o_TreeNode As TreeNode, ByVal o_HelpItem As HelpList) As TreeNode
        Dim o_CatNode As TreeNode = Nothing
        Dim o_ItemNode As TreeNode = Nothing
        '================
        'Get the category
        '================
        If o_TreeNode.Nodes.ContainsKey(o_HelpItem.Category) Then
            o_CatNode = o_TreeNode.Nodes.Item(o_HelpItem.Category)
        Else
            o_CatNode = o_TreeNode.Nodes.Add(o_HelpItem.Category, o_HelpItem.Category)
        End If
        o_CatNode.NodeFont = New Font("Arial", 10, FontStyle.Bold)
        '============
        'Add The Item
        '============
        If o_CatNode.Nodes.ContainsKey(o_HelpItem.HelpKey) Then
            o_ItemNode = o_CatNode.Nodes.Item(o_HelpItem.HelpKey)
        Else
            o_ItemNode = o_CatNode.Nodes.Add(o_HelpItem.HelpKey, o_HelpItem.Description)
        End If
        o_ItemNode.NodeFont = New Font("Arial", 10, FontStyle.Regular)
        '=========================
        'Set the Tag with the item
        '=========================
        o_ItemNode.Tag = o_HelpItem
        Return o_ItemNode
    End Function
    Private Sub HelpTree_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles HelpTree.DoubleClick
        '==================
        'Load the help item
        '==================
        If Me.HelpTree.SelectedNode IsNot Nothing Then
            If Me.HelpTree.SelectedNode.Tag IsNot Nothing Then
                Dim THelp As HelpList = CType(Me.HelpTree.SelectedNode.Tag, HelpList)
                If THelp IsNot Nothing Then
                    Dim TWeb As WebBrowser = LoadHelpPage(THelp.HelpKey, THelp.Description)
                    If THelp.IsLocal = True Then
                        TWeb.Navigate(IO.Path.Combine(HelpPath, THelp.Url))
                    Else
                        TWeb.Navigate(THelp.Url)
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub TSBRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBRefresh.Click
        'Get the active web Control
        Dim Tweb As WebBrowser = GetActiveWebControl()
        If Tweb IsNot Nothing Then
            Tweb.Refresh()
        End If
    End Sub

    Private Sub TSBBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBBack.Click
        'Get the active web Control
        Dim Tweb As WebBrowser = GetActiveWebControl()
        If Tweb IsNot Nothing Then
            Tweb.GoBack()
        End If
    End Sub
    Private Sub TSBForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBForward.Click
        'Get the active web Control
        Dim Tweb As WebBrowser = GetActiveWebControl()
        If Tweb IsNot Nothing Then
            Tweb.GoForward()
        End If
    End Sub

    Private Sub TSBClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBClose.Click
        If Me.TabControl1.SelectedTab IsNot Nothing Then
            Me.TabControl1.TabPages.Remove(Me.TabControl1.SelectedTab)
        End If
    End Sub

    Private Sub HelpTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles HelpTree.AfterSelect

    End Sub
End Class
